//
//  EquipmenCell.m
//  DataStatistics
//
//  Created by Kang on 15/12/10.
//  Copyright © 2015年 YTYangK. All rights reserved.
//

#import "EquipmenCell.h"
#import "EquipmenModel.h"
#import "SearchViewModel.h" //搜索ViewModel
#import "UIImage+GIF.h"

#define eCellNib [UINib nibWithNibName:@"EquipmenCell" bundle:nil]

@interface EquipmenCell ()

/** 设备型号 */
@property (strong, nonatomic) IBOutlet UILabel *equipmen;
@property (weak, nonatomic) IBOutlet UIImageView *stateimage;

@end


@implementation EquipmenCell

- (void)awakeFromNib {
    [super awakeFromNib];
    self.backgroundColor = RGB(33, 38, 44, 1);
    
    
    // Initialization code
    if (SYSTEM_VERSION_EQUAL_TO_W_orH(SCREEN_HEIGHT, @"568.0")) {
        self.DownTimeR.constant = 10;
        self.DownTimeTitleR.constant = 10;
        self.EquipmentW.constant = 150;
        self.NameLLayout.constant = 18;
        self.BtnL.constant = 60;
        self.stateLLayout.constant = 18;
        
    }else if (SYSTEM_VERSION_EQUAL_TO_W_orH(SCREEN_HEIGHT, @"667.0")){
  
        self.EquipmentW.constant = 190;

    }else{
        
        self.EquipmentW.constant = 220;

    }
}

+ (instancetype)initCellWithCellView:(UITableView *)tableView reuseIdentifier:(NSString *)identifier {
    // 因为identifier 与类名字一致
    EquipmenCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[[NSBundle mainBundle] loadNibNamed:identifier owner:self options:nil] firstObject];
        [tableView registerNib:eCellNib forCellReuseIdentifier:identifier];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;//取消点击效果
    return cell;
}


- (void)setModelCellData:(EListModel *)modelCellData {
    _modelCellData = modelCellData;
    [self setCellDataWithModel:_modelCellData];
    
}

- (void)setCellDataWithModel:(EListModel *)list {
    
    NSLog(@"%@",list.imie);
    
    if (list != nil) {
        
    self.equipmen.text  = [NSString stringWithFormat:@"%@",list.name];

        if (list.offLine ==nil) {

            self.stateimage.image = [UIImage imageNamed:@"B"];
            [self.stateimage setAnimationImages:@[[UIImage imageNamed:@"B"],[UIImage  imageNamed:@"C"]]];
            [self.stateimage setAnimationRepeatCount:-1];
            [self.stateimage setAnimationDuration:2.5];
            [self.stateimage startAnimating];
            //运行时间
            [self getOvertime:list.activityLife];
            self.Icon2.image = [UIImage imageNamed:@"信号亮"];

            if(list.state == 11){
                
                self.Icon1.image = [UIImage imageNamed:@"开机亮"];
                self.Icon3.image = [UIImage imageNamed:@"压力灰"];
                self.Icon4.image = [UIImage imageNamed:@"电机灰"];
          
                
                return;
            
            }else if(list.state ==12){
                
                self.Icon4.image = [UIImage imageNamed:@"电机灰"];
                self.Icon1.image = [UIImage imageNamed:@"开机亮"];

                NSString  *filePath1 = [[NSBundle bundleWithPath:[[NSBundle mainBundle] bundlePath]] pathForResource:@"GIF压力.gif" ofType:nil];
                NSData  *imageData1 = [NSData dataWithContentsOfFile:filePath1];
                self.Icon3.image = [UIImage sd_animatedGIFWithData:imageData1];

                return;
                
            }else if (list.state ==21){

                self.Icon1.image = [UIImage imageNamed:@"开机灰"];
                self.Icon3.image = [UIImage imageNamed:@"压力灰"];
                self.Icon4.image = [UIImage imageNamed:@"电机灰"];
                
                return;
                
            }else if (list.state ==22){
                
                self.Icon1.image = [UIImage imageNamed:@"开机灰"];
                self.Icon4.image = [UIImage imageNamed:@"电机灰"];
                NSString  *filePath = [[NSBundle bundleWithPath:[[NSBundle mainBundle] bundlePath]] pathForResource:@"GIF压力.gif" ofType:nil];
                NSData  *imageData = [NSData dataWithContentsOfFile:filePath];
                self.Icon3.image = [UIImage sd_animatedGIFWithData:imageData];
                
                return;

            }else if (list.state == 31){

                
                self.Icon3.image = [UIImage imageNamed:@"压力灰"];
                self.Icon1.image = [UIImage imageNamed:@"开机亮"];

                NSString  *filePath1 = [[NSBundle bundleWithPath:[[NSBundle mainBundle] bundlePath]] pathForResource:@"GIF电机.gif" ofType:nil];
                NSData  *imageData1 = [NSData dataWithContentsOfFile:filePath1];
                self.Icon4.image = [UIImage sd_animatedGIFWithData:imageData1];
                
                return;
                
            }else if (list.state == 32){

                self.Icon1.image = [UIImage imageNamed:@"开机亮"];

                NSString  *filePath1 = [[NSBundle bundleWithPath:[[NSBundle mainBundle] bundlePath]] pathForResource:@"GIF压力.gif" ofType:nil];
                NSData  *imageData1 = [NSData dataWithContentsOfFile:filePath1];
                self.Icon3.image = [UIImage sd_animatedGIFWithData:imageData1];
                NSString  *filePath2 = [[NSBundle bundleWithPath:[[NSBundle mainBundle] bundlePath]] pathForResource:@"GIF电机.gif" ofType:nil];
                NSData  *imageData2 = [NSData dataWithContentsOfFile:filePath2];
                self.Icon4.image = [UIImage sd_animatedGIFWithData:imageData2];
                
                
                return;
                
            }else if (list.state == 41){

                
                self.Icon3.image = [UIImage imageNamed:@"压力灰"];
                self.Icon1.image = [UIImage imageNamed:@"开机亮"];

                
                NSString  *filePath1 = [[NSBundle bundleWithPath:[[NSBundle mainBundle] bundlePath]] pathForResource:@"GIF电机.gif" ofType:nil];
                NSData  *imageData1 = [NSData dataWithContentsOfFile:filePath1];
                self.Icon4.image = [UIImage sd_animatedGIFWithData:imageData1];
                
                return;
                
                
            }else if (list.state == 42){

                self.Icon1.image = [UIImage imageNamed:@"开机亮"];
                NSString  *filePath1 = [[NSBundle bundleWithPath:[[NSBundle mainBundle] bundlePath]] pathForResource:@"GIF电机.gif" ofType:nil];
                NSData  *imageData1 = [NSData dataWithContentsOfFile:filePath1];
                self.Icon4.image = [UIImage sd_animatedGIFWithData:imageData1];        NSString  *filePath2 = [[NSBundle bundleWithPath:[[NSBundle mainBundle] bundlePath]] pathForResource:@"GIF压力.gif" ofType:nil];
                NSData  *imageData2 = [NSData dataWithContentsOfFile:filePath2];
                self.Icon3.image = [UIImage sd_animatedGIFWithData:imageData2];
                
                return;
            }else {
            
                self.Icon1.image = [UIImage imageNamed:@"开机灰"];
                self.Icon3.image = [UIImage imageNamed:@"压力灰"];
                self.Icon4.image = [UIImage imageNamed:@"电机灰"];
            
                return;
            }
            
        }else{

            self.stateimage.image = [UIImage imageNamed:@"A"];

            NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
            [formatter setDateStyle:NSDateFormatterMediumStyle];
            [formatter setTimeStyle:NSDateFormatterShortStyle];
            [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
            NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Shanghai"];
            [formatter setTimeZone:timeZone];
            
            NSDate* date = [formatter dateFromString:list.offLine];
            
            NSString *timeSp = [NSString stringWithFormat:@"%ld", (long)[date timeIntervalSince1970]];
            
            [self efficiency1:timeSp.intValue];
            
         }
     }
}


#pragma mark -设置Cell数据
//显示离线时间
-(void)efficiency1:(double)time{


    self.Icon1.image = [UIImage imageNamed:@"开机灰"];
    self.Icon2.image = [UIImage imageNamed:@"信号灰"];
    self.Icon3.image = [UIImage imageNamed:@"压力灰"];
    self.Icon4.image = [UIImage imageNamed:@"电机灰"];
    
    self.DownTimeTitle.text = @"最后离线时间";
    self.DownTimeTitle.textColor = [UIColor grayColor];
    self.DownTime.textColor = [UIColor grayColor];
    
    
    NSTimeInterval now = [[NSDate date]timeIntervalSince1970];
    double distanceTime = now - time;
    
    NSDate * beDate = [NSDate dateWithTimeIntervalSince1970:time];
    NSDateFormatter * df = [[NSDateFormatter alloc]init];
    [df setDateFormat:@"HH:mm"];
    NSString * timeStr = [df stringFromDate:beDate];
    
    [df setDateFormat:@"dd"];
    NSString * nowDay = [df stringFromDate:[NSDate date]];
    NSString * lastDay = [df stringFromDate:beDate];
    
    if(distanceTime <24*60*60 && [nowDay integerValue] == [lastDay integerValue]){//时间小于一天
        
        self.DownTime.text = timeStr;
        
    }else if(distanceTime<24*60*60*2 && [nowDay integerValue] != [lastDay integerValue]){
        
        if ([nowDay integerValue] - [lastDay integerValue] ==1 || ([lastDay integerValue] - [nowDay integerValue] > 10 && [nowDay integerValue] == 1)) {
            
            NSString *s = [NSString stringWithFormat:@"昨天 %@",timeStr];
            NSMutableAttributedString *str2 = [[NSMutableAttributedString alloc]initWithString:s];
            [str2 addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:13.0f] range:NSMakeRange(0,2)];
            self.DownTime.attributedText =str2;
            
        }else{
            [df setDateFormat:@"MM-dd"];
            self.DownTime.text = [df stringFromDate:beDate];
        }
        
    }else if(distanceTime <24*60*60*365){
        [df setDateFormat:@"MM-dd"];
        self.DownTime.text = [df stringFromDate:beDate];
        
    }else{
        [df setDateFormat:@"yyyy-MM-dd HH:mm"];
    }

}





/// 判断小标签是否有值
- (void)judgeBtnVlue:(int)btnVlue isDetermineClick:(BOOL)click {
    if (click) {
        self.btnVlue.hidden = click;
        [self.btnVlue setTitle:[NSString stringWithFormat:@"0"] forState:UIControlStateNormal];
    }else {
        if (btnVlue != 0) {
            self.btnVlue.hidden = NO;
            if (btnVlue > 99) {
                if (SYSTEM_VERSION_EQUAL_TO_W_orH(SCREEN_HEIGHT, @"736.0")) {
                    
                    self.BtnH.constant = 21;
                    self.BtnW.constant = 29;
                    self.btnVlue.titleLabel.font = [UIFont systemFontOfSize: 13];
                    
                }else {
                    self.BtnH.constant = 18;
                    self.BtnW.constant = 28;
                    self.btnVlue.titleLabel.font = [UIFont systemFontOfSize: 11.5];
                
                }
                [self.btnVlue setTitle:[NSString stringWithFormat:@"99+"] forState:UIControlStateNormal];
                [self.btnVlue setBackgroundImage:[UIImage imageNamed:@"icon-hongdian2"] forState:UIControlStateNormal];

            }else{
                if (SYSTEM_VERSION_EQUAL_TO_W_orH(SCREEN_HEIGHT, @"568.0")) {
                    self.BtnH.constant = 22;
                    self.BtnW.constant = 22;
                    self.btnVlue.titleLabel.font = [UIFont systemFontOfSize: 11];

                }else if (SYSTEM_VERSION_EQUAL_TO_W_orH(SCREEN_HEIGHT, @"667.0")){
                    self.BtnH.constant = 20;
                    self.BtnW.constant = 20;
                    self.btnVlue.titleLabel.font = [UIFont systemFontOfSize: 12];
                    
                }else if (SYSTEM_VERSION_EQUAL_TO_W_orH(SCREEN_HEIGHT, @"736.0")){
                    self.BtnH.constant = 22;
                    self.BtnW.constant = 22;
                    self.btnVlue.titleLabel.font = [UIFont systemFontOfSize: 13];
                }
            
                [self.btnVlue setTitle:[NSString stringWithFormat:@"%d",btnVlue] forState:UIControlStateNormal];
                [self.btnVlue setBackgroundImage:[UIImage imageNamed:@"icon-hongdian"] forState:UIControlStateNormal];
            }
        }else {
            self.btnVlue.hidden = YES;
        }
    }
}


- (void)getOvertime:(NSString*)mStr
{
    
    self.DownTimeTitle.text = @"运行时间";
    self.DownTimeTitle.textColor = [UIColor whiteColor];
    self.DownTime.textColor = [UIColor whiteColor];
    
    long msec = [mStr longLongValue];
    
  
        NSMutableAttributedString *str1 = [[NSMutableAttributedString alloc]initWithString:@"0小时"];
        
        [str1 addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:21.0f] range:NSMakeRange(0,1)];
        
        self.DownTime.attributedText = str1;
    
    NSInteger d = msec/60/60/24;
    NSInteger h = msec/60/60%24;
    //NSInteger  m = msec/1000/60%60;
    //NSInteger  s = msec/1000%60;
    
    NSString *_tStr = @"";
    NSString *_dStr = @"";
    NSString *_hStr = @"";
    
    _tStr = [NSString stringWithFormat:@"%ld天%ld小时",d,h];
    if (d > 0)
    {
        _dStr = [NSString stringWithFormat:@"%ld天",d];
    }
    if (h >= 0)
    {
        _hStr = [NSString stringWithFormat:@"%ld小时",h];
    }
    
    _tStr = [NSString stringWithFormat:@"%@%@",_dStr,_hStr];
    
    
    if (![_tStr isEqualToString:@""]) {
        NSString *len = [NSString stringWithFormat:@"%ld",d];
        NSString *len1 = [NSString stringWithFormat:@"%ld",h];
        
        NSMutableAttributedString *str1 = [[NSMutableAttributedString alloc]initWithString:_tStr];
        
        [str1 addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:21.0f] range:NSMakeRange(0,len.length)];
        
        [str1 addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:21.0f] range:NSMakeRange(_dStr.length,len1.length)];
        
        self.DownTime.attributedText =str1;
    }
    
    
}



//进入详情列表
- (IBAction)Details:(UIButton *)sender {
    
    [self.delegate didClickButton:sender];
    
    
}





@end
